package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/roles")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result page(String name,String label,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        PageResult<Role> pageResult = roleService.page(name,label,page,pageSize);
        return Result.success(pageResult);
    }


    @PostMapping
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getRole(@PathVariable Integer id){
        Role role = roleService.selectById(id);
        return Result.success(role);
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        List<Role> roleList = roleService.roleList();
        return Result.success(roleList);
    }

    @DeleteMapping("{id}")
    public Result deleteRole(@PathVariable Integer id){
        roleService.deleteRoleById(id);
        return Result.success();
    }






}
